# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import execjs
import os

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
------未登录状态只能查看20条---------
企业名片资讯
https://www.qimingpian.cn/finosda/project/pinvestment
"""

class Test_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "Accept": "application/json, text/plain, */*",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded",
            "DNT": "1",
            "Host": "vipapi.qimingpian.com",
            "Origin": "https://www.qimingpian.cn",
            "Pragma": "no-cache",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "cross-site",
            "User-Agent": ua.random
        }


    def get_contents(self):
        postData = {
            "time_interval":"",
            "tag":"",
            "tag_type":"",
            "province":"",
            "lunci":"",
            "page": "1",
            "num": "20",
            "unionid":""
        }
        url = "https://vipapi.qimingpian.com/DataList/productListVip"
        html = HttpUtils.do_request("POST", url,self.headers , postData)
        json_data = json.loads(html.text)
        encrypt_data = json_data["encrypt_data"]
        decrypt_data = self.get_decrypt(encrypt_data)
        json_data = json.loads(decrypt_data)
        for j in json_data['list']:
            product = j['product']  # 项目名称
            yewu = j['yewu']  # 业务
            hangye = j['hangye1']  # 行业
            province = j['province']  # 地区
            lunci = j['lunci']  # 轮次
            time = j['time']  # 投资时间
            money = j['money']  # 投资金额
            detail = j['detail']  # 链接详细
            heat_num = j['heat_num']  # 热度
            investor = ''  # 投资方
            for d in j['investor_info']:
                investor += d['investor'] + ";"

    # 解密
    def get_decrypt(self, encrypt_data):
        with open (os.getcwd() + "\js\decrypt.js", encoding='utf-8') as f:
            js = f.read()
        ctx =execjs.compile(js)
        decrypt = ctx.call("decrypt", encrypt_data)
        return decrypt

if __name__ == '__main__':
    test_get = Test_Get()
    test_get.get_contents()